Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(source): refine source macro #12260

Merged
merged 22 commits into from
Sep 19, 2023
Merged

Conversation

wenym1
Copy link
Contributor

@wenym1 wenym1 commented Sep 13, 2023

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

In our current code, we have many codes that define the whole method in macro to leverage the ability of list expansion inside macro to enumerate over all sources in a match statement, which is hard to read and debug, and the IDE has problem doing indexing on the code in the macro. Besides, when we want to support a new source, we need to add the new source to the list of both properties and split (before the previous refactor we even need to add to the list of reader and enumerator), which is somehow verbose.

In this PR, all sources are declared in a single macro for_all_classified_sources with the following definition.

macro_rules! for_all_classified_sources {
    ($macro:path $(,$extra_args:tt)*) => {
        $macro! {
            // cdc sources
            {
                { Mysql },
                { Postgres },
                { Citus }
            },
            // other sources
            {
                { Kafka, $crate::source::kafka::KafkaProperties, $crate::source::kafka::KafkaSplit },
                { Pulsar, $crate::source::pulsar::PulsarProperties, $crate::source::pulsar::PulsarSplit },
                { Kinesis, $crate::source::kinesis::KinesisProperties, $crate::source::kinesis::split::KinesisSplit },
                { Nexmark, $crate::source::nexmark::NexmarkProperties, $crate::source::nexmark::NexmarkSplit },
                { Datagen, $crate::source::datagen::DatagenProperties, $crate::source::datagen::DatagenSplit },
                { GooglePubsub, $crate::source::google_pubsub::PubsubProperties, $crate::source::google_pubsub::PubsubSplit },
                { Nats, $crate::source::nats::NatsProperties, $crate::source::nats::split::NatsSplit },
                { S3, $crate::source::filesystem::S3Properties, $crate::source::filesystem::FsSplit }
            }
            $(
                ,$extra_args
            )*
        }
    };
}

Based on for_all_classified_sources, we reimplement the current dispatch_source_prop macro for enumerating ConnectorProperties enum, and implement dispatch_split_impl macro for enumerating SplitImpl. With these macros, many methods of ConnectorProperties and SplitImpl previously defined in macro can be implemented in the same way as normal methods. When we want to support a new source, we only need to add its information in the for_all_classified_sources macro.

The end of a series of source refactor. PRs before:

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

@codecov
Copy link

codecov bot commented Sep 13, 2023

Codecov Report

Merging #12260 (d7ca600) into main (a5b596f) will increase coverage by 0.00%.
Report is 1 commits behind head on main.
The diff coverage is 40.82%.

@@           Coverage Diff           @@
##             main   #12260   +/-   ##
=======================================
  Coverage   69.73%   69.74%           
=======================================
  Files        1424     1424           
  Lines      236449   236444    -5     
=======================================
+ Hits       164888   164897    +9     
+ Misses      71561    71547   -14     
Flag Coverage Δ
rust 69.74% <40.82%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/connector/src/lib.rs 81.25% <ø> (ø)
src/connector/src/source/cdc/split.rs 17.77% <0.00%> (ø)
src/connector/src/source/filesystem/file_common.rs 0.00% <0.00%> (ø)
src/connector/src/source/google_pubsub/split.rs 0.00% <0.00%> (ø)
src/connector/src/source/kafka/split.rs 77.41% <0.00%> (ø)
src/connector/src/source/kinesis/split.rs 3.44% <0.00%> (ø)
src/connector/src/source/nats/split.rs 0.00% <0.00%> (ø)
src/connector/src/source/nexmark/split.rs 18.18% <0.00%> (ø)
src/connector/src/source/pulsar/split.rs 0.00% <0.00%> (ø)
src/meta/src/stream/source_manager.rs 46.29% <0.00%> (+1.09%) ⬆️
... and 4 more

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@StrikeW StrikeW left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally LGTM, thanks for the PR.

Copy link
Contributor

@tabVersion tabVersion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your effort! Great work!

@wenym1 wenym1 added this pull request to the merge queue Sep 19, 2023
Merged via the queue into main with commit c8ea5ee Sep 19, 2023
@wenym1 wenym1 deleted the yiming/refactor-source-macro branch September 19, 2023 11:50
@xxchan
Copy link
Member

xxchan commented Oct 18, 2023

and the IDE has problem doing indexing on the code in the macro

Does this PR help mitigate this issue? 🤔

macro_rules! dispatch_source_prop {
($impl:expr, $source_prop:tt, $body:expr) => {{
use $crate::source::ConnectorProperties;
$crate::dispatch_source_enum! {ConnectorProperties, { $impl }, {$source_prop, IgnorePropType, IgnoreSplitType}, {$body}}
Copy link
Contributor

@StrikeW StrikeW Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I revisit this pr to want to understand how does dispatch_source_prop! work, but it is hard to get it. Because these macros are nested instead of in a flatten structure.
For example to understand the third arg {$source_prop, IgnorePropType, IgnoreSplitType}, I need to dive into other macros level by level. What's worse is that IDE can't expand these macro_rules! directly without calling it in a function. Is there a way to show the expanded code of a macro_rules! without calling it just like a function definition? Or coud we flatten these macros a bit? cc @wenym1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to show the expanded code of a macro_rules! without calling it just like a function definition

You can write an example in a random function and use cargo expand to see the expanded code.

coud we flatten these macros a bit

This main idea of this PR is to declare all source related information in only one place so that when developing a new source, we don't have to declare these information in places previously scattered around in our codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants